import { invokeRPC } from "../helpers";
import type { XHSCommentList, XHSMention, XHSNote } from "@common/models";
import { XHSComment } from "@common/models/xhs/notes";

// const inInject = initRpcInInject(invokeRPC);

// export const getXS = (params: {
//   sessionId: string;
//   category: keyof XHSSession["x-s"];
// }) =>
//   invokeRPC({
//     rpcName: "app.getXhsXS",
//     receiver: "plugin",
//     rpcParams: params,
//   }) as Promise<{ xs: string; common: string }>;
//
// export const loadMentions = inInject.loadMentions;
// export const loadNotes = inInject.loadNotes;
// export const loadLikes = inInject.loadLikes;
// export const loadConnections = inInject.loadConnections;

export const loadAllMentions = (params?: { cursor?: number }) =>
  invokeRPC({
    rpcName: "app.loadXHSMentions",
    rpcParams: params ?? {},
    receiver: "plugin",
  }) as Promise<{
    hasMore: boolean;
    records: XHSMention[];
  }>;

export const getNote = (params: {
  targetId?: string;
  noteId: string;
  xsecToken: string;
}) =>
  invokeRPC({
    rpcName: "app.getNoteDetail",
    rpcParams: params,
    receiver: "plugin",
  }) as Promise<XHSNote>;

export const loadCommentsInNote = (params: {
  targetId: string;
  noteId: string;
  xsecToken: string;
  isPageInited?: boolean;
}) =>
  invokeRPC({
    rpcName: "app.loadNoteComments",
    rpcParams: params,
    receiver: "plugin",
  }) as Promise<XHSCommentList>;

export const loadMoreSubCommentsInNote = (params: {
  targetId: string;
  noteId: string;
  xsecToken: string;
  rootCommentId: string;
}) =>
  invokeRPC({
    rpcName: "app.loadSubNoteComments",
    rpcParams: params,
    receiver: "plugin",
  }) as Promise<XHSCommentList>;

export const replyInNote = (params: {
  targetId: string;
  noteId: string;
  xsecToken: string;
  commentId?: string;
  content: string;
}) =>
  invokeRPC({
    rpcName: "app.replyInNote",
    rpcParams: params,
    receiver: "plugin",
  }) as Promise<XHSComment>;

export const replyToCommentInNote = (params: {
  targetId: string;
  sessionId: string;
  noteId: string;
  xsecToken: string;
  comment?: XHSComment;
  content: string;
}) =>
  invokeRPC({
    rpcName: "app.replyToComment",
    rpcParams: params,
    receiver: "plugin",
  }) as Promise<XHSComment>;

export const getEmojis = (params: {
  targetId: string;
  noteId: string;
  xsecToken: string;
}) =>
  invokeRPC({
    rpcName: "app.getEmojis",
    rpcParams: params,
    receiver: "plugin",
  }) as Promise<Array<{ name: string; img: string }>>;

export const markMentionAsReaded = (params: { id: XHSMention["id"] }) => {
  return invokeRPC({
    rpcName: "app.markMentionAsReaded",
    rpcParams: params,
    receiver: "plugin",
  });
};

export const getMyRepliesInNote = (params: { noteId: XHSNote["noteId"] }) => {
  return invokeRPC({
    rpcName: "app.getMyRepliesInNote",
    rpcParams: params,
    receiver: "plugin",
  }) as Promise<Array<XHSComment>>;
};

export const syncMention = (params: { sessionId: string }) => {
  return invokeRPC({
    rpcName: "app.syncMention",
    rpcParams: params,
    receiver: "plugin",
  }) as Promise<XHSMention[] | void>;
};
